﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using UnityEngine;
using UnityEngine.UI;

[Editor]
public class Panel : MonoBehaviour
{
   [HideInInspector] public PanerlManager panerlManager;

    public Text text;
    /// <summary>
    /// 实际坐标点
    /// </summary>
    public Vector2 point;

    /// <summary>
    /// 参考坐标点
    /// </summary>
    public Vector2 basePoint;
    
    private string panelId;//需要保证id的唯一性



    [Header("基于该方块的基础上解锁")]  public Panel baseLockPanel;
    

    public float money;

    public int cubeNum;
    
    public bool IsShow;
    

    /// <summary>
    /// 设置板的Id
    /// </summary>
    /// <param name="name"></param>
    public void SetPanelId(string panelId)
    {
        this.panelId = panelId;
        this.name = "panel" + panelId;
    }

    /// <summary>
    /// 获取当前Panel 的 id
    /// </summary>
    /// <returns></returns>
    public string GetPanelId()
    {
        return this.panelId;
    }
    
    
    /// <summary>
    /// 设置坐标点
    /// </summary>
    /// <param name="basePoint"></param>
    /// <param name="pos"></param>
    public void SetPoint(Vector2 basePoint, Vector2 pos)
    {
        this.point = pos;
        this.basePoint = basePoint;
    }
    

    /// <summary>
    /// 导出数据
    /// </summary>
    /// <returns></returns>
    public DataClass ExportData()
    {
        DataClass dataClass = new DataClass();

        dataClass.Id = GetPanelId();//当前的面板的ID

        if (baseLockPanel == null)
        {
            dataClass.baseLockId = "";
        }
        else
        {
            dataClass.baseLockId = baseLockPanel.GetPanelId();//需求的面板的ID
        }
        
        //需要解锁的方块
        dataClass.cubeNum = this.cubeNum;
       
        //上锁类型
        dataClass.lockType = lockType;
        dataClass.money = money;
        
        //是否展示(开局就展示)
        dataClass.isShow = this.IsShow;
        
        //位置
        dataClass.basePoint = basePoint;
        dataClass.point = point;
       
        dataClass.panelType = panelType;
        dataClass.roadType = roadType;
        
        return dataClass;
    }

    private DataClass _dataClass;

    public DataClass GetDataClass => this._dataClass;

    /// <summary>
    /// 加载数据
    /// </summary>
    /// <param name="dataClass"></param>
    public void LoadData(DataClass dataClass)
    {
        panelId = dataClass.Id;
        basePoint = dataClass.basePoint;
        point = dataClass.point;
        lockType = dataClass.lockType;
        money = dataClass.money;
        panelType = dataClass.panelType;
        roadType = dataClass.roadType;
        IsShow = dataClass.isShow;
        this._dataClass = dataClass;
        this.cubeNum = dataClass.cubeNum;
        OnValidate();
    }

    /// <summary>
    /// 设置
    /// </summary>
    /// <param name="baseLockPanel"></param>
    public void SetBaseLockPanel(Panel baseLockPanel)
    {
        this.baseLockPanel = baseLockPanel;
    }
    
    
    
    [Serializable]
    public class DataClass
    {

        /// <summary>
        /// 标识当前土块的名字
        /// </summary>
        public string Id;


        /// <summary>
        /// 基于上上一块的解锁 id
        /// </summary>
        public string baseLockId;
        
        /// <summary>
        /// 参考坐标点
        /// </summary>
        public Vector2 basePoint;
        /// <summary>
        /// 坐标点
        /// </summary>
        public Vector2 point;

        /// <summary>
        /// 锁类型
        /// </summary>
        public LockType lockType;

        /// <summary>
        /// 解锁需要的前
        /// </summary>
        public float money;

        /// <summary>
        /// 需要解锁多少方块才能解锁
        /// </summary>
        public int cubeNum;
        
        /// <summary>
        /// 底座类型
        /// </summary>
        public PanelType panelType;

        
        /// <summary>
        /// 路面类型
        /// </summary>
        public RoadType roadType;
        
        
        /// <summary>
        /// 是否展示
        /// </summary>
        public bool isShow;
        
    }
    

    /// <summary>
    /// 获取坐标点
    /// </summary>
    public Vector2 GetPoint => point - basePoint;

    public void OnValidate()
    {
        //只有地面才能搞东西
        if (panelType != PanelType.Road)
        {
            roadType = RoadType.None;
        }

        if (panelType == PanelType.Water)
        {
            text.gameObject.SetActive(false);
        }
        else
        {
            text.gameObject.SetActive(true);
        }

        if (lockType == LockType.Coin)
        {
            text.color = Color.black;
        }
        else
        {
            text.color = Color.white;
        }
        
        
        GetComponent<Image>().sprite = panerlManager.GetSpriteBy(roadType);
        GetComponent<Image>().color = GetColor();

        if (cubeNum > 0)
        {
            text.text = money +"/ <color=red>"+ cubeNum+"</color>";
        }
        else
        {
            text.text = money.ToString();
        }
        
        
    }

    public void OnDrawGizmosSelected()
    {
        Gizmos.color = Color.green;
        Gizmos.DrawWireCube(transform.position,GetComponent<RectTransform>().sizeDelta);
    }
    
    
    
    //获取颜色
    public Color GetColor()
    {
        switch (panelType)
        {
            case PanelType.Road:
                if(roadType==RoadType.None)
                    return Color.green;
                else
                    return Color.yellow;
            case PanelType.Bridge:
                return Color.yellow;
            case PanelType.Other:
                return  Color.gray;
            default:
                return Color.clear;//没有颜色
        }
    }


    /// <summary>
    /// 解锁方式
    /// </summary>
    public LockType lockType;

    /// <summary>
    /// 地面类型
    /// </summary>
    public PanelType panelType;


    /// <summary>
    /// 地面产物
    /// </summary>
    public RoadType roadType;
    
    
   
    
    
    
    
    
    
}
